﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delonix_Regia_HMS
{
    public partial class CheckInOut : Form
    {
        public CheckInOut()
        {
            InitializeComponent();
        }

        int bookingId = 0;
        int initialRoomId = 0;
        int updatedRoomId = 0;
        DateTime actualCheckInDate = DateTime.MinValue;
        string actualCheckInTime = null;

        #region Check In Management
        private void onlyNumbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void btnCheckInManagementSearchByCheckInId_Click(object sender, EventArgs e)
        {
            tbxCheckInManagementSearchCustomerName.Clear();
            tbxCheckInManagementSearchNric.Clear();
            if (string.IsNullOrEmpty(tbxCheckInManagementSearchCheckInId.Text))
            {
                MessageBox.Show("Please enter a CheckIn Id to search!");
            }
            else
            {
                DBManager.DisplayAllCheckInByCheckInId(listViewCheckInManagementCheckInInformation, Convert.ToInt32(tbxCheckInManagementSearchCheckInId.Text));
            }
        }

        private void btnCheckInManagementDisplayAllCheckIn_Click(object sender, EventArgs e)
        {
            tbxCheckInManagementSearchCustomerName.Clear();
            tbxCheckInManagementSearchNric.Clear();
            tbxCheckInManagementSearchCheckInId.Clear();

            DBManager.DisplayAllCheckIn(listViewCheckInManagementCheckInInformation);
        }

        private void btnCheckInManagementSearchByCustomerNric_Click(object sender, EventArgs e)
        {
            tbxCheckInManagementSearchCheckInId.Clear();
            tbxCheckInManagementSearchCustomerName.Clear();
            if (string.IsNullOrEmpty(tbxCheckInManagementSearchNric.Text))
            {
                MessageBox.Show("Please enter a customer NRIC to search!");
            }
            else
            {
                DBManager.DisplayAllCheckInByCustomerNric(listViewCheckInManagementCheckInInformation, tbxCheckInManagementSearchNric.Text);
            }
        }

        private void btnCheckInManagementSearchByCustomerName_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbxCheckInManagementSearchCustomerName.Text))
            {
                MessageBox.Show("Please enter a customer first name");
            }
            else
            {
                DBManager.DisplayAllCheckInByCustomerFirstName(listViewCheckInManagementCheckInInformation, tbxCheckInManagementSearchCustomerName.Text);
            }

        }

        private void listViewCheckInInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCheckInManagementCheckInInformation.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listViewCheckInManagementCheckInInformation.SelectedItems)
                {
                    initialRoomId = Convert.ToInt32(listViewCheckInManagementCheckInInformation.SelectedItems[0].SubItems[2].Text);
                    bookingId = Convert.ToInt32(listViewCheckInManagementCheckInInformation.SelectedItems[0].SubItems[1].Text);
                    
                    DBManager.FillComboxBoxRoomID(cbxCheckInManagementRoomId, bookingId);
                    cbxCheckInManagementRoomId.Items.Add(initialRoomId);
                    tbxCheckInManagementCheckInId.Text = listViewCheckInManagementCheckInInformation.SelectedItems[0].Text;
                    tbxCheckInManagementBookingId.Text = listViewCheckInManagementCheckInInformation.SelectedItems[0].SubItems[1].Text;
                    cbxCheckInManagementRoomId.Text = listViewCheckInManagementCheckInInformation.SelectedItems[0].SubItems[2].Text;
                    actualCheckInDate = Convert.ToDateTime(listViewCheckInManagementCheckInInformation.SelectedItems[0].SubItems[3].Text);
                    actualCheckInTime = listViewCheckInManagementCheckInInformation.SelectedItems[0].SubItems[4].Text;
                }
            }
            DBManager.DisplayBookingsByBookingIdForCheckInUpdate(listViewCheckInManagementBookingInformation, bookingId);
        }

        private void btnCheckInManagementCheckInCustomer_Click(object sender, EventArgs e)
        {
            CustomerCheckIn customerCheckIn = new CustomerCheckIn();
            customerCheckIn.ShowDialog();
        }

        private void btnCheckInManagementConfirmUpdate_Click(object sender, EventArgs e)
        {
            bool successfulUpdate = false;
            if (string.IsNullOrEmpty(tbxCheckInManagementCheckInId.Text))//Check if there is a checkIn Id
            {
                MessageBox.Show("Please Select a CheckIn to Update!");
            }
            else if (string.IsNullOrEmpty(tbxCheckInManagementBookingId.Text))
            {
                MessageBox.Show("Please Select a existing booking");
            }
            else if (string.IsNullOrEmpty(cbxCheckInManagementRoomId.Text))
            {
                MessageBox.Show("Please Select a Room to Check the Customer In!");
            }
            else if(actualCheckInDate ==  DateTime.MinValue)
            {
                MessageBox.Show("Please select an existing Check In to Update!");
            }
            else if(string.IsNullOrEmpty(actualCheckInTime))
            {
                MessageBox.Show("Please select and existing Check In to Update!");
            }
            else //  Everything filled up
            {
                
                CheckIn cI = new CheckIn();
                cI.CheckInId = Convert.ToInt32(tbxCheckInManagementCheckInId.Text);
                cI.BookingId = Convert.ToInt32(tbxCheckInManagementBookingId.Text);
                cI.RoomId = Convert.ToInt32(cbxCheckInManagementRoomId.Text);
                cI.ActualCheckInDate = actualCheckInDate;
                cI.ActualCheckInTime = actualCheckInTime;
                DBManager.UpdateCheckIn(cI);
                successfulUpdate = true;
                DBManager.UpdateRoomStatusCheckInRoomIdUpdate(initialRoomId, updatedRoomId);
            }
            if (successfulUpdate == true)
            {
                MessageBox.Show("Update Successful!");
            }
            else
            {
                MessageBox.Show("Update NOT Successful!");
            }
        }

        #endregion
        #region Check Out Management
        private void btnCheckOutManagementSearchByCheckOutInvoiceId_Click(object sender, EventArgs e)
        {
            tbxCheckOutManagementSearchCheckInId.Clear();
            tbxCheckOutManagementSearchCustomerNRIC.Clear();
            tbxCheckOutManagementSearchCustomerFirstName.Clear();
            if (string.IsNullOrEmpty(tbxCheckOutManagementSearchCheckOutInvoiceId.Text))
            {
                MessageBox.Show("Please enter a Check Out Invoice Id to search!");
            }
            else
            {
                DBManager.DisplayAllCheckOutByCheckOutId(listViewCheckOutManagementCheckOutInformation, Convert.ToInt32(tbxCheckOutManagementSearchCheckOutInvoiceId.Text));
            }
        }

        private void btnCheckOutManagementSearchByCheckInId_Click(object sender, EventArgs e)
        {
            tbxCheckOutManagementSearchCheckOutInvoiceId.Clear();
            tbxCheckOutManagementSearchCustomerNRIC.Clear();
            tbxCheckOutManagementSearchCustomerFirstName.Clear();
            if (string.IsNullOrEmpty(tbxCheckOutManagementSearchCheckInId.Text))
            {
                MessageBox.Show("Please enter a Check In Id to search!");
            }
            else
            {
                DBManager.DisplayAllCheckOutByCheckInId(listViewCheckOutManagementCheckOutInformation, Convert.ToInt32(tbxCheckOutManagementSearchCheckInId.Text));
            }
        }

        private void btnCheckOutManagementSearchByCustomerNric_Click(object sender, EventArgs e)
        {
            tbxCheckOutManagementSearchCheckOutInvoiceId.Clear();
            tbxCheckOutManagementSearchCheckInId.Clear();
            tbxCheckOutManagementSearchCustomerFirstName.Clear();
            if (string.IsNullOrEmpty(tbxCheckOutManagementSearchCustomerNRIC.Text))
            {
                MessageBox.Show("Please enter a Customer NRIC to Search!");
            }
            else
            {
                DBManager.DisplayAllCheckOutByCustomerNric(listViewCheckOutManagementCheckOutInformation, tbxCheckOutManagementSearchCustomerNRIC.Text);
            }
        }

        private void btnCheckOutManagementSearchByCustomerFirstName_Click(object sender, EventArgs e)
        {
            tbxCheckOutManagementSearchCheckOutInvoiceId.Clear();
            tbxCheckOutManagementSearchCheckInId.Clear();
            tbxCheckOutManagementSearchCustomerNRIC.Clear();
            if (string.IsNullOrEmpty(tbxCheckOutManagementSearchCustomerFirstName.Text))
            {
                MessageBox.Show("Please enter a Customer First Name to Search!");
            }
            else
            {
                DBManager.DisplayAllCheckOutByCustomerFirstName(listViewCheckOutManagementCheckOutInformation, tbxCheckOutManagementSearchCustomerFirstName.Text);
            }
        }

        private void btnCheckOutManagementDisplayAllCheckOut_Click(object sender, EventArgs e)
        {
            tbxCheckOutManagementSearchCheckOutInvoiceId.Clear();
            tbxCheckOutManagementSearchCheckInId.Clear();
            tbxCheckOutManagementSearchCustomerFirstName.Clear();
            tbxCheckOutManagementSearchCustomerNRIC.Clear();
            DBManager.DisplayAllCheckOut(listViewCheckOutManagementCheckOutInformation);
        }

        #endregion

        private void listViewCheckOutManagementCheckOutInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCheckOutManagementCheckOutInformation.SelectedItems.Count > 0)
            {
                DBManager.DisplayAllItemsConsumeByCheckInId(listViewItemConsumeInformation, Convert.ToInt32(listViewCheckOutManagementCheckOutInformation.SelectedItems[0].SubItems[1].Text));
            }
        }

        private void btnCheckOutCustomer_Click(object sender, EventArgs e)
        {
            CustomerCheckOut cO = new CustomerCheckOut();
            cO.ShowDialog();
        }

        private void cbxCheckInManagementRoomId_SelectedIndexChanged(object sender, EventArgs e)
        {
            updatedRoomId = Convert.ToInt32(cbxCheckInManagementRoomId.Text);
        }
    }
}
